package com.pachong.frameworkdemo.demo.httpdemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.annotations.SerializedName;
import com.pachong.android.baseuicomponent.activity.PullableListActivity;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.framework.httprequest.HttpRequest;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.framework.httprequest.responsecallbacks.ListDataRequestListener;
import com.pachong.android.frameworkbase.imageloader.ImageLoader;
import com.pachong.android.frameworkbase.imageloader.ImageLoaderCallback;
import com.pachong.frameworkdemo.R;


/**
 * 刷列表的模板
 * <p>
 * 主要分为三步：fragment  + adapter(+ layout布局)  + bean
 * <p>
 * 注意事项:
 * 1. IHttpRequest request = new HttpRequest(this, mRules);
 * MyResponseCallback cb = new MyResponseCallback(this, mRules);
 * 这两个要记得带上mRules
 * <p>
 * 2. adapter要设置 泛型参数（LoginResponce）  e.g extends RecyclerViewBaseAdapter<LoginResponce>
 */
public class RequestDemoListActivity extends PullableListActivity {

//    private final static String URL_TEST = "http://hpapi.baoruan.com:10081/test/list";
private final static String URL_TEST = "http://apitest.baoruan.com/launcher/default/index?version=9.150320&un=239BA6301606E596F70AC1E7B4808B23&name=recommendlist&channelId=ceshi_baoruanservice1";

    private int mPage = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        mPage = 0;
        clearDecorations();
        setPullToRefreshEnable(false);
    }

    @Override
    public void onResume() {
        super.onResume();
        startLoading();
    }

    private ListDataRequestListener<TemplateBean> mResponceListener = new ListDataRequestListener<>(this, TemplateBean.class);

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new TemplateListAdapter(this);
    }

    @Override
    public void onStartLoading() {
        if (mResponceListener.getResponseDataParser().hasMore()) {
            mPage++;
        }
        UrlParams params = new UrlParams();
        params.put("page", ""+mPage);

        IHttpRequest request = new HttpRequest(this);
        request.get(URL_TEST, params, mResponceListener);

//        request.get(URL_TEST, params, new RequestListener<LoginResponce>(LoginResponce.class) {
//            @Override
//            public void onSuccess(LoginResponce o) {
//
//            }
//
//            @Override
//            public void onFailure(Throwable t, int errorNo, String strMsg) {
//
//            }
//        });


    }




    /**
     * Adapter
     */
    public class TemplateListAdapter extends RecyclerViewBaseAdapter<TemplateBean> {

        public TemplateListAdapter(Context con) {
            super(con);
        }

        @Override
        public RecyclerView.ViewHolder onCreateHolder(ViewGroup viewGroup, int i) {
            return new ItemViewHolder(setAdapterViewItem(viewGroup, R.layout.listitem_template));
        }


        @Override
        public void onBindHolder(RecyclerView.ViewHolder viewHolder, int i) {
            ItemViewHolder holder = (ItemViewHolder) viewHolder;
            final TemplateBean bean = getData().get(i);
//            ImageLoader.with(RequestDemoListActivity.this).load(bean.getIconUrl()).into(holder.ivIcon).start();
            ImageLoaderCallback cb = new ImageLoaderCallback() {
                @Override
                public void onSuccess(Bitmap bmp) {
                    Log.e("vivi", "load success");
                }

                @Override
                public void onFailure() {
                    Log.e("vivi", "load onFailure");
                }
            };
            ImageLoader.with(RequestDemoListActivity.this).load(bean.getIconUrl()).callback(cb).start();
            holder.tvText.setText(bean.getText());
        }

        public class ItemViewHolder extends RecyclerView.ViewHolder {
            public RelativeLayout lytMain;
            public TextView tvText;
            public ImageView ivIcon;

            public ItemViewHolder(View itemView) {
                super(itemView);
                lytMain = (RelativeLayout) itemView.findViewById(R.id.lytMain);
                ivIcon = (ImageView) itemView.findViewById(R.id.ivIcon);
                tvText = (TextView) itemView.findViewById(R.id.tvText);
            }
        }
    }


    /**
     * 数据对象 Bean
     */
    public static class TemplateBean {
        @SerializedName("icoUrl")
        String icoUrl;

        String name;

        public String getIconUrl() {
            return icoUrl;
        }

        public void setIconUrl(String iconUrl) {
            this.icoUrl = iconUrl;
        }

        public String getText() {
            return name;
        }

        public void setText(String text) {
            this.name = text;
        }
    }

}
